/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet.edt;

import bdd.DBLink;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrateur
 */
public class DeleteOldCrenau extends HttpServlet {

    public static final String DELETE_CRENAU = "DELETE FROM CRENEAU WHERE NUMSALLE = ? AND NUMENSEIGNANT = ? AND NUMTYPE = ? AND NUMEC = ? AND HORAIREDEBUT = ? AND HORAIREFIN = ? AND IDDATE = ? AND NUMPROMOTION = ?";

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String ec = (request.getParameter("ec") == null) ? "" : request.getParameter("ec");
            String type = (request.getParameter("type") == null) ? "" : request.getParameter("type");
            String jour = (request.getParameter("jour") == null) ? "" : request.getParameter("jour");
            String salle = (request.getParameter("salle") == null) ? "" : request.getParameter("salle");
            String enseignant = (request.getParameter("enseignant") == null) ? "" : request.getParameter("enseignant");
            int heureDebut = (request.getParameter("heureDebut") == null) ? 0 : Integer.parseInt(request.getParameter("heureDebut"));
            String minuteDebut = (request.getParameter("minuteDebut") == null) ? "00" : request.getParameter("minuteDebut");
            int heureFin = (request.getParameter("heureFin") == null) ? 0 : Integer.parseInt(request.getParameter("heureFin"));
            String minuteFin = (request.getParameter("minuteFin") == null) ? "00" : request.getParameter("minuteFin");
            int promotion = (request.getParameter("promotion") == null) ? 0 : Integer.parseInt(request.getParameter("promotion"));

            if (ec.equals("")
                    || type.equals("")
                    || jour.equals("")
                    || salle.equals("")
                    || enseignant.equals("")
                    || (heureDebut == 0 && minuteDebut.equals("00"))
                    || (heureFin == 0 && minuteFin.equals("00"))
                    || ((heureDebut * 60) + Integer.parseInt(minuteDebut)) >= ((heureFin * 60) + Integer.parseInt(minuteFin))
                    || promotion == 0) {
                out.println("<p> <font color=\"red\"> Données incomplètes pour la suppression </font> </p>");
            } else {

                DBLink dbLink = new DBLink();
                dbLink.connexion();

                jour += " 00:00:00,000000000";

                dbLink.setPst(dbLink.getConnection().prepareStatement(DELETE_CRENAU));
                dbLink.getPst().setString(1, salle);
                dbLink.getPst().setString(2, enseignant);
                dbLink.getPst().setString(3, type);
                dbLink.getPst().setString(4, ec);
                dbLink.getPst().setString(5, heureDebut + "" + minuteDebut);
                dbLink.getPst().setString(6, heureFin + "" + minuteFin);
                dbLink.getPst().setString(7, jour);
                dbLink.getPst().setString(8, Integer.toString(promotion));
                dbLink.getPst().executeQuery();

                out.println("<p> <font color=\"green\"> Le crénau a bien été supprimé </font> </p>");
            }
        } catch (Exception e) {
            e.printStackTrace(out);
            out.println("<p> <font color=\"red\"> Suite à un problèmem technique, vote demande ne peut être traitée </font> </p>");
            out.println("<p> Détails : " + e.getMessage() + "</p>");
        } finally {
            out.close();
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

    }// </editor-fold>
}
